home *** CD-ROM | disk | FTP | other *** search
/ The Java 3D API Specification (2nd Edition) / The Java 3D API Specification (2nd Edition).iso / programs / examples / GeometryByReference / ImageComponentByReferenceTest.java < prev    next >
Text File  |  2000-04-28  |  9KB  |  310 lines

  1. /*
  2.  *    @(#)ImageComponentByReferenceTest.java 1.8 00/03/31 14:31:00
  3.  *
  4.  * Copyright (c) 1996-2000 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Sun grants you ("Licensee") a non-exclusive, royalty free, license to use,
  7.  * modify and redistribute this software in source and binary code form,
  8.  * provided that i) this copyright notice and license appear on all copies of
  9.  * the software; and ii) Licensee does not utilize the software in a manner
  10.  * which is disparaging to Sun.
  11.  *
  12.  * This software is provided "AS IS," without a warranty of any kind. ALL
  13.  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
  14.  * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
  15.  * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE
  16.  * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
  17.  * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS
  18.  * LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
  19.  * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
  20.  * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
  21.  * OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
  22.  * POSSIBILITY OF SUCH DAMAGES.
  23.  *
  24.  * This software is not designed or intended for use in on-line control of
  25.  * aircraft, air traffic, aircraft navigation or aircraft communications; or in
  26.  * the design, construction, operation or maintenance of any nuclear
  27.  * facility. Licensee represents and warrants that it will not use or
  28.  * redistribute the Software for such purposes.
  29.  */
  30.  
  31. import java.applet.Applet;
  32. import java.awt.*;
  33. import java.awt.event.*;
  34. import com.sun.j3d.utils.applet.MainFrame;
  35. import com.sun.j3d.utils.geometry.*;
  36. import com.sun.j3d.utils.universe.*;
  37. import javax.media.j3d.*;
  38. import javax.vecmath.*;
  39. import javax.swing.*;
  40. import javax.swing.event.*;
  41. import javax.swing.border.*;
  42. import com.sun.j3d.utils.behaviors.mouse.*;
  43. import com.sun.j3d.utils.image.TextureLoader;
  44. import com.sun.j3d.utils.geometry.Sphere;
  45. import com.sun.j3d.utils.geometry.Box;
  46. import com.sun.j3d.utils.geometry.ColorCube;
  47. import java.awt.image.*;
  48. import java.awt.color.ColorSpace;
  49. import com.sun.j3d.utils.geometry.*;
  50.  
  51. public class ImageComponentByReferenceTest extends JApplet implements ActionListener {
  52.  
  53.     Shape3D s1,s2;
  54.     TextureLoader t0, t1, t2;
  55.     int count = 0;
  56.  
  57.     Appearance app = new Appearance();
  58.     BranchGroup objRoot = new BranchGroup();
  59.     TransformGroup objTrans = new TransformGroup();
  60.     BufferedImage bImage1;
  61.     TiledImage checkBoard;
  62.     boolean yUp = false;
  63.     boolean byRef = true;
  64.     JComboBox rasterType, texType;
  65.     ImageComponent2D[] image = new ImageComponent2D[8];
  66.     Appearance dummyApp = new Appearance();
  67.     Texture2D texOne, texCheckBoard;
  68.     javax.media.j3d.Raster raster;
  69.     Box textureCube;
  70.     Shape3D boxShape;
  71.     int w1 = 64, h1 = 32, checkw = 16 , checkh = 16;
  72.  
  73.     private java.net.URL texImage = null;
  74.     
  75.     public BranchGroup createSceneGraph() {
  76.         objRoot = new BranchGroup();
  77.  
  78.         objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
  79.         objTrans.setCapability(Group.ALLOW_CHILDREN_WRITE);
  80.  
  81.         objRoot.addChild(objTrans);
  82.  
  83.         BoundingSphere bounds =
  84.             new BoundingSphere(new Point3d(0.0,0.0,0.0), 100.0);
  85.  
  86.     app.setCapability(Appearance.ALLOW_TEXTURE_WRITE);
  87.  
  88.  
  89.         textureCube = new Box(0.4f, 0.4f, 0.4f,
  90.                                   Box.GENERATE_TEXTURE_COORDS|
  91.                   Box.GENERATE_NORMALS, app);
  92.     boxShape = textureCube.getShape(Box.FRONT);
  93.     boxShape.setCapability(Shape3D.ALLOW_APPEARANCE_WRITE);
  94.     objTrans.addChild(textureCube);
  95.  
  96.     checkBoard = new TiledImage();
  97.     TextureLoader texLoader = new TextureLoader( texImage, this);
  98.     ImageComponent2D oneImage = texLoader.getImage();
  99.     bImage1 = oneImage.getImage();
  100.  
  101.     int index = 0;
  102.     image[index++] =  new ImageComponent2D(oneImage.getFormat(),
  103.                       (RenderedImage)bImage1,
  104.                       false,
  105.                       true);
  106.  
  107.     image[index++] =  new ImageComponent2D(oneImage.getFormat(),
  108.                       (RenderedImage)bImage1,
  109.                       true,
  110.                       true);
  111.  
  112.  
  113.     image[index++] =  new ImageComponent2D(oneImage.getFormat(),
  114.                       (RenderedImage)bImage1,
  115.                       false,
  116.                       false);
  117.     
  118.     
  119.     image[index++] =  new ImageComponent2D(oneImage.getFormat(),
  120.                       (RenderedImage)bImage1,
  121.                       true,
  122.                       false);
  123.  
  124.     createRaster(objRoot);
  125.  
  126.     image[index++] =  new ImageComponent2D(ImageComponent.FORMAT_RGBA,
  127.                       checkBoard,
  128.                       false,
  129.                       true);
  130.  
  131.     image[index++] =  new ImageComponent2D(ImageComponent.FORMAT_RGBA,
  132.                       checkBoard,
  133.                       true,
  134.                       true);
  135.  
  136.  
  137.     image[index++] =  new ImageComponent2D(ImageComponent.FORMAT_RGBA,
  138.                       checkBoard,
  139.                       false,
  140.                       false);
  141.     
  142.     
  143.     image[index++] =  new ImageComponent2D(ImageComponent.FORMAT_RGBA,
  144.                       checkBoard,
  145.                       true,
  146.                       false);
  147.  
  148.  
  149.  
  150.     texOne = new Texture2D(Texture.BASE_LEVEL,
  151.                       Texture.RGBA,
  152.                       image[2].getWidth(), image[2].getHeight());
  153.  
  154.     texOne.setCapability(Texture.ALLOW_IMAGE_WRITE);
  155.     texOne.setImage(0, image[2]);
  156.     
  157.         app.setTexture(texOne);
  158.  
  159.     texCheckBoard = new Texture2D(Texture.BASE_LEVEL,
  160.                       Texture.RGBA,
  161.                       image[4].getWidth(), image[4].getHeight());
  162.     
  163.     texCheckBoard.setCapability(Texture.ALLOW_IMAGE_WRITE);
  164.         objRoot.compile();
  165.     return objRoot;
  166.     }
  167.  
  168.     public void actionPerformed(ActionEvent e ) {
  169.     Object target = e.getSource();
  170.  
  171.     if (target == rasterType) {
  172.         if (rasterType.getSelectedIndex() < 4) {
  173.         raster.setSize(w1, h1);
  174.         }
  175.         else {
  176.         raster.setSize(checkw, checkh);
  177.         }
  178.         raster.setImage(image[rasterType.getSelectedIndex()]);
  179.     }
  180.     else if (target == texType) {
  181.         boxShape.setAppearance(dummyApp);
  182.         if (texType.getSelectedIndex() < 4) {
  183.         texOne.setImage(0, image[texType.getSelectedIndex()]);
  184.         app.setTexture(texOne);
  185.         }
  186.         else {
  187.         texCheckBoard.setImage(0, image[texType.getSelectedIndex()]);
  188.         app.setTexture(texCheckBoard);
  189.         }
  190.         
  191.         boxShape.setAppearance(app);
  192.     }
  193.  
  194.  
  195.     }
  196.  
  197.     JPanel createImagePanel() {
  198.     JPanel panel = new JPanel();
  199.     String texVals[] = { "One_Yup_ByCopy",
  200.                  "One_Yup_ByReference",
  201.                  "One_Ydown_ByCopy",
  202.                  "One_Ydown_ByReference",
  203.                  "Checkered_Yup_ByCopy",
  204.                  "Checkered_Yup_ByReference",
  205.                  "Checkered_Ydown_ByCopy",
  206.                  "Checkered_Ydown_ByReference"};
  207.     
  208.     rasterType = new JComboBox(texVals);
  209.     rasterType.setLightWeightPopupEnabled(false);
  210.     rasterType.addActionListener(this);
  211.     rasterType.setSelectedIndex(2);
  212.     panel.add(new JLabel("Raster Image"));     
  213.     panel.add(rasterType);
  214.  
  215.     texType = new JComboBox(texVals);
  216.     texType.setLightWeightPopupEnabled(false);
  217.     texType.addActionListener(this);
  218.     texType.setSelectedIndex(2);
  219.     panel.add(new JLabel("Texture Image"));     
  220.     panel.add(texType);
  221.     return panel;
  222.  
  223.     }
  224.  
  225.  
  226.  
  227.     public ImageComponentByReferenceTest() 
  228.     {
  229.     }
  230.  
  231.     public ImageComponentByReferenceTest(java.net.URL url) {
  232.         texImage = url;
  233.     }
  234.  
  235.     public void init() {
  236.         if (texImage == null) {
  237.         // the path to the image for an applet
  238.         try {
  239.             texImage = new java.net.URL(getCodeBase().toString() +
  240.                         "../images/one.jpg");
  241.         }
  242.         catch (java.net.MalformedURLException ex) {
  243.             System.out.println(ex.getMessage());
  244.         System.exit(1);
  245.         }
  246.     }
  247.  
  248.         Canvas3D c = new Canvas3D(SimpleUniverse.getPreferredConfiguration());
  249.     BranchGroup scene = createSceneGraph();
  250.         SimpleUniverse u = new SimpleUniverse(c);
  251.         u.getViewingPlatform().setNominalViewingTransform();
  252.         u.addBranchGraph(scene);
  253.     Container contentPane = getContentPane();
  254.     JPanel p = new JPanel();
  255.     BoxLayout boxlayout = new BoxLayout(p, 
  256.                         BoxLayout.Y_AXIS);
  257.     p.setLayout(boxlayout);
  258.         contentPane.add("Center", c);
  259.   
  260.     contentPane.add("South", p);
  261.     
  262.     p.add(createImagePanel());
  263.  
  264.     }
  265.  
  266.     public static void main(String[] args) {
  267.         java.net.URL url = null;
  268.         // the path to the image file for an application
  269.     try {
  270.         url = new java.net.URL("file:../images/one.jpg");
  271.     }
  272.     catch (java.net.MalformedURLException ex) {
  273.         System.out.println(ex.getMessage());
  274.         System.exit(1);
  275.     }
  276.  
  277.         new MainFrame(new ImageComponentByReferenceTest(url), 800, 700);
  278.     }
  279.  
  280.    void createRaster( BranchGroup scene) {
  281.  
  282.  
  283.  
  284.     // Create raster geometries and shapes
  285.     Vector3f trans = new Vector3f( );
  286.     Transform3D tr = new Transform3D( );
  287.     TransformGroup tg;
  288.  
  289.     // Left
  290.     raster = new javax.media.j3d.Raster( );
  291.     raster.setCapability(javax.media.j3d.Raster.ALLOW_IMAGE_WRITE);
  292.     raster.setCapability(javax.media.j3d.Raster.ALLOW_SIZE_WRITE);
  293.     raster.setPosition( new Point3f( -0.9f, 0.75f, 0.0f ) );
  294.     raster.setType( javax.media.j3d.Raster.RASTER_COLOR );
  295.     raster.setOffset( 0, 0 );
  296.  
  297.     raster.setSize( image[2].getWidth(), image[2].getHeight() );
  298.     raster.setImage( image[2] );
  299.     Shape3D sh = new Shape3D( raster, new Appearance( ) );
  300.     scene.addChild( sh );
  301.  
  302.  
  303.  
  304.     }
  305. }
  306.  
  307.  
  308.  
  309.  
  310.